/*
THIS FILE PREDICTS EXCESS RETURNS of DIFFERENT HORIZONS USING THE VARIABLES DESCRIBED IN THE PAPER
*/
use "$datapath/Prediction_Differential.dta", clear // the 73 countries

// select sample
drop if datem < ym(1970,1)
drop if datem > ym(2019,2)
drop if country == "unitedstates"

************************************************************
*** regressions
global controls "dus_r dus_ln_dy dus_ln_ep"

** TABLE 1
reghdfe dus_r1 dus_r, 			absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r1 dus_ln_dy, 		absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r1 dus_ln_ep, 		absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r1 dus_r dus_ln_ep, absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r1 dus_r dus_ln_dy, absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r1 $controls , 		absorb(i.id , savefe) vce(cluster datem) resid

** TABLE 2
reghdfe dus_r36 $controls , absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r24 $controls, absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r12 $controls, absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r1  $controls, absorb(i.id , savefe) vce(cluster datem) resid

** TABLE A1
run "$finalpath/codes/_do_estim/0_countryselection" // we predict based on the 35 countries solely
reghdfe dus_r24d90 	$controls, absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r36d90 	$controls, absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r12d90 	$controls, absorb(i.id , savefe) vce(cluster datem) resid
reghdfe dus_r1 		$controls, absorb(i.id , savefe) vce(cluster datem) resid
************************************************************

************************************************************
* sample for predictions
drop if datem > ym(2016,7)
* first month by country
gen month = mofd(dofm(datem))
cap drop id
sencode country, gen(id)
gen t = month - 118

*****************************************************************************
* loops
global horizon = "24d89 24d90 24d92 24d925 24d93 24d94 12d90 36d93 "

* differentials that matter
foreach h of global horizon {
	display as text "`h'" // keep track of loop 
	qui xtset id datem
	
	*** ER + DY + EP
	display as text "ER + DY + EP"
	forvalues j = 300(1)560 { // loop over months 

		qui reghdfe dus_r`h' $controls  if t <= `j', absorb(i.id , savefe) vce(cluster datem) resid // regress recursively
		qui predict xbplusfe_`j', xbd	
	}
	
	qui gen er_`h' = .
	forvalues j = 300(1)560 {
		qui replace er_`h' = xbplusfe_`j' if t == `j'
	}
	qui drop xbplusfe_*	
	
}
*****************************************************************************

*****************************************************************************
* NON RECURSIVE
qui reghdfe dus_r24d94 $controls, absorb(i.id , savefe) vce(cluster datem) resid
qui predict er_NONREC_24d94, xbd // fitted values + fixed effects
*****************************************************************************

*****************************************************************************
* replace 0 by missings
foreach v of varlist er_* {
	qui replace `v' = . if `v' == 0
}
*****************************************************************************

* variables kept outside iteration
keep datem country er_*
save "$datapath/Portfolio_Paper_Predictions.dta", replace
* end
*****************************************************************************